package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.dto.UserRegDto;
import cn.tedu.springmvc.util.JsonResult;
import cn.tedu.springmvc.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import static cn.tedu.springmvc.util.JsonResult.State.ERR_PASSWORD;
import static cn.tedu.springmvc.util.JsonResult.State.ERR_USERNAME;

@RestController
@RequestMapping("/user")
public class UserController {
    public UserController() {
        System.out.println("UserController.UserController");
    }

    @GetMapping("/npe.do")
    public String npe(String username){
            username=username.toString();
            System.out.println(username);
        return "HAHAHA";
    }


    @RequestMapping("/login.do")
    public JsonResult<Void> login(String username,String password){
        System.out.println("username = " + username + ", password = " + password);
        // 假设 admin / 888888 是正确的 用户名 / 密码
        if ("admin".equals(username)){
            //用户名正常,判断密码
            if ("888888".equals(password)){
                //密码也正确,登陆成功
                return JsonResult.ok();
            }else {
                //密码错误
                String message = "登陆失败,密码错误!";
                return JsonResult.fail(ERR_PASSWORD,message);
            }
        }else {
            //用户名不存在
            String message = "登陆失败,用户名不存在!";
            return JsonResult.fail(ERR_USERNAME,message);
        }


    }

    @RequestMapping("/reg.do")
    public JsonResult<Void> reg(UserRegDto userRegDto){
        System.out.println("userRegDto = " + userRegDto);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/info.do")
    public JsonResult<UserVO> info(@PathVariable Integer id){
        System.out.println("即将查询 id = " + id + " 的用户的信息……");
        UserVO userVO = new UserVO();
        userVO.setUsername("chengheng");
        userVO.setPassword("1234567890");
        userVO.setEmail("chengheng@qq.com");
        return JsonResult.ok(userVO);
    }

    @GetMapping("/{username:[a-zA-Z]+}/info.do")
    public UserVO info(@PathVariable String username) {
        System.out.println("即将查询 用户名 = " + username + " 的用户的信息……");
        UserVO userVO = new UserVO();
        userVO.setUsername("chengheng");
        userVO.setPassword("1234567890");
        userVO.setEmail("chengheng@qq.com");
        return userVO;
    }

    @GetMapping("/list/info.do")
    public UserVO list(){
        System.out.println("即将查询 用户的列表 的信息……");
        return null;
    }

}
